package com.example.cnotebook.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.example.cnotebook.R;
import com.example.cnotebook.pojo.NoteInfo;
import com.example.cnotebook.util.UserConstant;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

public class HomeListViewAdapter extends BaseAdapter {

    private Context context;
    private ArrayList<NoteInfo> noteInfos;
    private Date date = null;
    private SimpleDateFormat format = new SimpleDateFormat("yyyy年MM月");
    private SimpleDateFormat formatTime = new SimpleDateFormat("yyyy.MM.dd");

    public HomeListViewAdapter(Context context, ArrayList<NoteInfo> noteInfos) {
        this.context = context;
        this.noteInfos = noteInfos;
    }

    @Override
    public int getCount() {
        return noteInfos.size();
    }

    @Override
    public Object getItem(int position) {
        return noteInfos.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.item_home_layout, null);
            holder = new ViewHolder();
            holder.tv_date = convertView.findViewById(R.id.tv_date);
            holder.tv_title = convertView.findViewById(R.id.tv_title);
            holder.tv_content = convertView.findViewById(R.id.tv_content);
            holder.tv_dateTime = convertView.findViewById(R.id.tv_dateTime);
            holder.tv_hasAudio = convertView.findViewById(R.id.tv_hasAudio);
            holder.tv_category = convertView.findViewById(R.id.tv_category);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        NoteInfo noteInfo = noteInfos.get(position);
        holder.tv_title.setText(noteInfo.getTitle());
        holder.tv_content.setText(noteInfo.getContent());
        Date createDateTime = noteInfo.getCreateDateTime();
        holder.tv_dateTime.setText(formatTime.format(createDateTime));
        holder.tv_category.setText(noteInfo.getCategoryName());
        Date date = noteInfo.getCreateDate();
        if (date == null) {
            holder.tv_date.setVisibility(View.GONE);
        } else {
            holder.tv_date.setText(format.format(date));
            holder.tv_date.setVisibility(View.VISIBLE);
        }
        if (noteInfo.getHasAudio() == 0) {
            holder.tv_hasAudio.setVisibility(View.GONE);
        } else {
            holder.tv_hasAudio.setVisibility(View.VISIBLE);
        }
        return convertView;
    }

    static class ViewHolder {
        TextView tv_date, tv_title, tv_content, tv_dateTime, tv_category, tv_hasAudio;
    }
}
